//
//  MainTabBarViewController.m
//  webBgShangCheng
//
//  Created by tanw on 16/6/23.
//  Copyright © 2016年 tanw. All rights reserved.
//

#import "TabButton.h"
#import "MainTabBarViewController.h"

#import "HomeViewController.h"
#import "guangboViewController.h"
#import "ecarViewController.h"
#import "MyallViewController.h"


typedef NS_ENUM(NSInteger, MenuType){
    kMenuTypeHome,            //主页
    kMenuTypeNearbyBusinesses,// 附近商家
    KMenuTypeShoppingCart,    // 购物车
    kMenuTypePersonalCenter   // 个人中心

};

@interface MainTabBarViewController ()

// 底部tabbar
@property (nonatomic, strong) UIImageView *tabBarView;

@property (nonatomic, strong) TabButton *currentTabButton;

@end
#import "BaseViewController.h"

@implementation MainTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //-----------------------------------------------------
    [self setupTabBarView];
    [self.view setBackgroundColor:[UIColor whiteColor]];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self removeWildTabBar];
    
}

-(void)setHideTabBar:(BOOL) isHide{
    [self.tabBarView setHidden:isHide];
}


// 移除掉原生的TabBar,使用我们自己的
- (void)removeWildTabBar {
    for (UIView* obj in self.tabBar.subviews) {
        if (obj != self.tabBarView) {
            [obj removeFromSuperview];
        }
    }
}

- (void)setupTabBarView {
    self.tabBarView = [[UIImageView alloc]initWithFrame:self.tabBar.bounds];
    self.tabBarView.userInteractionEnabled = YES;
    self.tabBarView.backgroundColor = [UIColor whiteColor];
    [self.tabBar.items makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.tabBar addSubview:self.tabBarView];
    // tab顶部分割线
    UIView *divider = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                               0,
                                                     Screen_Width,
                                                               0.5)];
    divider.backgroundColor = TSColor(23, 200, 180);
    [self.tabBarView addSubview:divider];
    // TODO: 这里的图片需要替换
    [self creatButtonWithNormalName:@"snacknav1"
                      andSelectName:@"snacknav1_on"
                           andTitle:@"首页"
                        andMenuType:kMenuTypeHome];
    
    [self creatButtonWithNormalName:@"snacknav2"
                      andSelectName:@"snacknav2_on"
                           andTitle:@"广播"
                        andMenuType:kMenuTypeNearbyBusinesses];
    [self creatButtonWithNormalName:@"snacknav3"
                      andSelectName:@"snacknav3_on"
                           andTitle:@"购物车"
                        andMenuType:KMenuTypeShoppingCart];
    
 
    [self creatButtonWithNormalName:@"snacknav4"
                      andSelectName:@"snacknav4_on"
                           andTitle:@"我"
                        andMenuType:kMenuTypePersonalCenter];

    
    HomeViewController *homeCollectionView = [[HomeViewController alloc] init];
    UINavigationController *homeCollectionNavView = [[UINavigationController alloc] initWithRootViewController:homeCollectionView];
    
    guangboViewController *nearbyBusinessesView = [[guangboViewController alloc] init];
    UINavigationController *nearbyBusinessesNavView = [[UINavigationController alloc] initWithRootViewController:nearbyBusinessesView];
    
    ecarViewController *ShoppingCarView = [[ecarViewController alloc]init];
    UINavigationController *ShoppingCarNavView = [[UINavigationController alloc] initWithRootViewController:ShoppingCarView];
    
    
    MyallViewController *personalCenterView = [[MyallViewController alloc] init];
    
    
    self.viewControllers = @[homeCollectionNavView,nearbyBusinessesNavView,ShoppingCarNavView,personalCenterView];
}

- (void)creatButtonWithNormalName:(NSString *)normal
                    andSelectName:(NSString *)selected
                         andTitle:(NSString *)title
                      andMenuType:(MenuType)menuType {
    TabButton * customButton = [TabButton buttonWithType:UIButtonTypeCustom];
    customButton.tag = menuType;
    CGFloat buttonWidth = self.tabBarView.frame.size.width / 4;
    CGFloat buttonHeight = self.tabBarView.frame.size.height+2;
    customButton.frame = CGRectMake(buttonWidth*menuType, 0, buttonWidth, buttonHeight);
 
    
    [customButton setImage:[UIImage imageNamed:normal]
                  forState:UIControlStateNormal];
    
    //这里应该设置选中状态的图片
    [customButton setImage:[UIImage imageNamed:selected]
                  forState:UIControlStateSelected];
    [customButton setTitle:title forState:UIControlStateNormal];
    [customButton addTarget:self
                     action:@selector(changeViewController:)
           forControlEvents:UIControlEventTouchUpInside];
    customButton.imageView.contentMode = UIViewContentModeCenter;
    customButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    customButton.titleLabel.font = [UIFont systemFontOfSize:10];
    [customButton setTitleColor:[UIColor grayColor]
                       forState:UIControlStateNormal];
    [customButton setTitleColor:TSColor(1, 199, 166)
                       forState:UIControlStateSelected];
    [self.tabBarView addSubview:customButton];
    if(menuType == kMenuTypeHome) {
        self.currentTabButton = customButton;
        self.currentTabButton.selected = YES;
    }
    [self changeViewController:self.currentTabButton];
    customButton.exclusiveTouch = YES;
}

- (void)changeViewController:(TabButton *)tabButton {
    if(self.selectedIndex != tabButton.tag){ // wsq
        self.selectedIndex = tabButton.tag;  // 切换不同控制器的界面
        self.currentTabButton.selected = ! self.currentTabButton.selected;
        self.currentTabButton = tabButton;
        self.currentTabButton.selected = YES;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)setupNavigation {
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *image = [UIImage imageNamed:@"back_icon"];
    backButton.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    [backButton setImage:image forState:UIControlStateNormal];
    [backButton addTarget:self
                   action:@selector(back)
         forControlEvents:UIControlEventTouchUpInside];
   // [backButton setEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:backButton];
    [self.navigationItem setLeftBarButtonItem:item];
    UINavigationBar *bar = self.navigationController.navigationBar;
    [bar setBarTintColor:[UIColor whiteColor]];
    [self removeBottomLineInNavigation];
    if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
        CGRect statusBarViewFrame = CGRectMake(0, -20, self.view.frame.size.width, 20);
        UIView *statusBarView = [[UIView alloc] initWithFrame:statusBarViewFrame];
        statusBarView.backgroundColor=[UIColor whiteColor];
        [self.navigationController.navigationBar addSubview:statusBarView];
        self.navigationController.navigationBar.translucent = NO;
    }
}

// 移除UINavigationBar底部的黑线
- (void)removeBottomLineInNavigation {
    NSArray *subViews = [self.navigationController.navigationBar subviews];
    if ([subViews count] > 0) {
        UIView *view = subViews[0];
        NSArray *ssbviews = [view subviews];
        NSInteger count = [ssbviews count];
        if (count > 0) {
            for (NSInteger i=0; i<count; i++) {
                UIView *ssbView = ssbviews[i];
                if ([ssbView isKindOfClass:[UIImageView class]]) {
                    [ssbView removeFromSuperview];
                }
            }
        }
    }
}

-(void)setTitle:(NSString *)title {
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0 , 100, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];  //设置Label背景透明
    titleLabel.font = [UIFont systemFontOfSize:17];  //设置文本字体与大小
    titleLabel.textColor = [UIColor whiteColor];  //设置文本颜色
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = title;  //设置标题
    self.navigationItem.titleView = titleLabel;
}

- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

-(UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)dealloc
{
    //TRACE(@"deallocing");
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}
@end
